<?php

class Router {

    public static function post() {

        $host = explode(":", $_SERVER["HTTP_HOST"]);
        if ($host[0] == "localhost") {
            $script_name = explode("/", $_SERVER["SCRIPT_NAME"]);
            $script_length = strlen($script_name[1]);
            $request_uri = substr($_SERVER['REQUEST_URI'], $script_length + 2);
            $request_uri1 = substr($_SERVER['REQUEST_URI'], $script_length + 1);
            $arr_uri = explode("/", $request_uri);
            if (substr($_SERVER['REQUEST_URI'], -1) != '/') {
                header('Location: ' . $_SERVER['REQUEST_URI'] . '/');
            }
            $urlRoutes = array(
                "/^\/{$arr_uri[0]}\/?[a-zA-Z]*\/$/" => APP_PATH."/modules/{$arr_uri[0]}/index" . EXT,
            );
            foreach ($urlRoutes as $expression => $controllerName) {
                
                if (preg_match($expression, $request_uri1)) {
                    $controller = $controllerName;
                }
            }
            if (!empty($controller)) {
                if (is_file($controller)) {
                    require $controller;
                } else {
                    require 'controllers/pagenotfound.php';
                    $controller = 'PageNotFound';
                }
            } else {
                require 'controllers/pagenotfound.php';
                $controller = 'PageNotFound';
            }
        }
    }

}

?>
